<?php
include_once("../Dao/db.php");
Class Entity
{
	var $objectType = 1; //1:SerachEngine;2:Query
	
	// 開始時間
	var $fromDate;
	// 結束時間
	var $toDate;
	// 毎ページのレコード数
	var $RECORD_NUMBER_PER_PAGE = 20;
	
	var $nowPageNum = 1;
	
	var $media=-1;
	
	var $mediaList="<option value='-1' >(指定なし)";
	
	var $camp=-1;
	
	var $campList="<option value='-1' >(指定なし)";
	
	var $group=-1;
	
	var $groupList="<option value='-1' >(指定なし)";
	
	var $keyword=-1;
	
	var $keywordList="<option value='-1' >(指定なし)";

	var $pageNum=0;
	
	var $searchName="";

	var $htmlTable ;
	
	
	
	
	function MyTask()
	{
		session_start();
		if($_POST["act"] == null)
		{
			$this->getMeida();
			$this->getCamp();
			if($_GET["campaignId"] != null)
			{
				$this->camp = $_GET["campaignId"];
				$this->fromDate = $_GET["fromDate"];
				$this->toDate = $_GET["toDate"];
				$this->setMediaList($this->camp);				
				$this->getGroup();
				$this->group = $_GET["adgroupId"];
				$this->getKeyword();
				$this->keyword = $_GET["keywordId"];												
			}
		}
		else if($_POST["act"] == "getGroup")
		{
			$this->iniForm();
			$this->getMeida();
			$this->getCamp();
			$this->setMediaList($this->camp);			
			$this->getGroup();
			$this->group = "-1";			
		}
		else if($_POST["act"] == "getKeyWord")
		{
			$this->iniForm();
			$this->getMeida();
			$this->getCamp();
			$this->getGroup();
			$this->getKeyword();
			$this->keyword = "-1";
		}
		else
		{
			$this->iniForm();			
			$this->getList();
			$this->getSelectSql();
		}
	}
	
	function Entity()
	{
		$this->fromDate = date("Y-m-01",time());
		$this->toDate = date("Y-m-d",time());
	}
	
	function iniForm()
	{	
		if(isset($_GET["obj"])){
			$this->objectType=$_GET["obj"];
		}
		
		if(isset($_GET["keywordId"])){
			$this->keyword = $_GET["keywordId"];
		}
		
		// 開始日
		if(isset($_POST["from_date"])){
			$this->fromDate = $_POST["from_date"];
		}else{
			$this->fromDate = date("Y-m-01",time());
		}
		// 結束日
		if(isset($_POST["to_date"])){
			$this->toDate = $_POST["to_date"];
		}else{
			$this->toDate = date("Y-m-d",time());
		}
		
		if(isset($_POST["search_media_select"])) $this->media = $_POST["search_media_select"];
		//if(isset($_POST["search_campaign_select"])) $this->camp = $_POST["search_campaign_select"];
		//if(isset($_POST["search_banner_select"])) $this->group = $_POST["search_banner_select"];
		
		//if(isset($_POST["search_keyword_select"])){
		//	$this->keyword = $_POST["search_keyword_select"];
		//}
		if($_POST["isMediaSelectClick"] == "1")
		{
			$this->camp = -1;
			$this->group = -1;
			$this->keyword = -1;						
		}
		else
		{
			$this->camp = $_POST["search_campaign_select"];
			$this->group = $_POST["search_banner_select"];
			$this->keyword = $_POST["search_keyword_select"];			
		}
		if(isset($_POST["search_text"])){
			$this->searchName = $_POST["search_text"];
		}
				
		//$this->nowPageNum = $_POST["nowPage"];				
	}
	
	function getSelectSql()
	{	
		$html = "";
		
		if($this->objectType == 1){
			$sql = "select name,SUM(clickCnt) from ";
			$sql .= " searchenginereport as R join searchengine  as S on  R.SearchEngine_Id = S.id where";
		}else{
			$sql = "select query,SUM(clickCnt)  from queryreport as R where ";
		}
		
		$sql .= " MemberID=".$_SESSION["memberId"];
			
		$sql .= " AND TO_DAYS('".$this->fromDate."')<=TO_DAYS(date) ";
		$sql .= " And TO_DAYS(date)<=TO_DAYS('".$this->toDate."') ";
			
		if($this->media != -1) $sql .= " AND R.listingType=".$this->media;
		if($this->camp != -1) $sql .= " AND campaign_id=".$this->camp;
		if($this->group != -1) $sql .= " AND adgroup_id=".$this->group;
		if($this->keyword != -1) $sql .= " AND keyword_id=".$this->keyword;
		
		if($this->objectType == 1){
			if($this->searchName != "") $sql .= " AND name like '%".$this->searchName."%'";
			$sql.= " group by name ";
		}else{ 
			if($this->searchName != "") $sql .= " AND query like '%".$this->searchName."%'";
			$sql.= " group by query ";
		}
		
		echo("<!--".$sql."-->");
		$result = mysql_query($sql);

		$rows = mysql_num_rows($result);
		$html .='<table width="100%" border="0" cellpadding="4" cellspacing="0" class="tbl">';
  		$html .='<tr align="right" class="tr2">';
		if($this->objectType == 1){
    		$html .='<td align="left">検索エンジン </td>';
		}else{
			$html .='<td align="left">クエリワード</td>';
		}
    	$html .='<td>Click</td>';
  		$html .='</tr>';
		
		$this->pageNum = $rows;
		if ($rows > 0)
		{
			$total = 0;
			for ($i=0;$i<$rows;$i++)
			{
				$data = mysql_fetch_row($result);				
				//$this->mediaList = $this->mediaList."<option id=".$i." value=".$data[0].">".$data[1]."</option>";
				$total += $data[1];
				if($i%2 == "0"){
					$html .= '<tr align="right" class="tr3"><td align="left">'.$data[0].'</td><td>'.$data[1].'</td></tr>';
				}else{
					$html .= '<tr align="right" class="tr4"><td align="left">'.$data[0].'</td><td>'.$data[1].'</td></tr>';
				}
			}
			$html .= '<tr align="right" class="tr2"><td align="left">【合計】</td><td>'.$total.'</td></tr>';			
		}else{
			$html .= '<tr align="right" class="tr2"><td align="left" colspan="2">データはありません！</td></tr>';			
		}
		
		
		
		
		$html.='</table>';
		
		
		$html.='<table width="100%" border="0" cellpadding="2" cellspacing="0"><tr><td align="left">';
		$html.='</td><td align="right">1件-'.$this->pageNum.'件/ '.$this->pageNum.'件&nbsp;&nbsp;';
		$html.='</td></tr></table><br>';
		$this->htmlTable = $html;
		//echo $html;
	}

	function getList()
	{
		$this->getMeida();
		$this->getCamp();
		$this->getGroup();
		$this->getKeyword();
	}
	
	function getMeida()
	{
		$sql = "select id,name from listingmst";
		//$sql .="where memberId = ".$_SESSION["memberId"]." ";
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		if ($rows > 0)
		{
			for ($i=0;$i<$rows;$i++)
			{
				$data = mysql_fetch_row($result);				
				$this->mediaList = $this->mediaList."<option id=".$i." value=".$data[0].">".$data[1]."</option>";
			}			
		}
	}
	
	function getCamp()
	{
		$sql = "select camp_Id,name from campaign ";
		$sql .="where memberId = ".$_SESSION["memberId"]." ";
		//echo $sql;
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		if ($rows > 0)
		{
			for ($i=0;$i<$rows;$i++)
			{
				$data = mysql_fetch_row($result);				
				$this->campList = $this->campList."<option id=".$i." value=".$data[0].">".$data[1]."</option>";
			}			
		}
	}
	
	function getGroup()
	{
		$sql = "select adGr_Id,name from adgroup ";
		$sql .="where memberId = ".$_SESSION["memberId"]." and camp_ID=".$this->camp;
		//echo $sql;
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		if ($rows > 0)
		{
			for ($i=0;$i<$rows;$i++)
			{
				$data = mysql_fetch_row($result);				
				$this->groupList = $this->groupList."<option id=".$i." value=".$data[0].">".$data[1]."</option>";
			}			
		}		
	}
	
	function getKeyword()
	{
		$sql = "select key_Id,text from keyword ";
		$sql .="where memberId = ".$_SESSION["memberId"]." and adGr_Id=".$this->group;
		//echo $sql;
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		if ($rows > 0)
		{
			for ($i=0;$i<$rows;$i++)
			{
				$data = mysql_fetch_row($result);				
				$this->keywordList = $this->keywordList."<option id=".$i." value=".$data[0].">".$data[1]."</option>";
			}			
		}
	}
	
	function setMediaList($campaignId)
	{
		session_start();
		$sql = "select listingType from campaign where camp_ID=".$campaignId." and MemberID=".$_SESSION["memberId"];
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		if ($rows > 0)
		{
			$data = mysql_fetch_row($result);
			$this->media = $data[0];
		}
		else
		{
			$this->searchMediaSelect = "-1";
		}
	}	
				
}

?>